var express = require('express');
// 创建了一个路由器
var router = express.Router();

const shoppingCartApi = require("../api/shoppingCart")


// 获取购物车列表数据
router.get('/', async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await shoppingCartApi.get({ curPage, eachPage });
  res.send(data);
});


// 删除商品 
router.delete('/delComm', async function (req, res, next) {
  const { _id } = req.body;
  const data = await shoppingCartApi.delComm(_id);
  res.send(data);
});


// 获取新增购物车数据
router.get('/getAddCartData', async function (req, res, next) {
  const { user_id, seckill_id } = req.query;
  const data = await shoppingCartApi.getAddCartData({ user_id, seckill_id });
  res.send(data);
});

// 新增购物车
router.post('/addComm', async function (req, res, next) {
  const { user_id, seckill_id, count } = req.body;
  const data = await shoppingCartApi.addComm({ user_id, seckill_id, count });
  res.send(data);
});


// 修改商品数量
router.patch('/changeCount', async function (req, res, next) {
  console.log("in");
  const { count, _id } = req.body;
  const data = await shoppingCartApi.changeCount({ count, _id });
  res.send(data);
});

// unipp页面修改商品数量(unipp的request不能发送patch请求)
router.put('/PutChangeCount', async function (req, res, next) {
  const { count, _id } = req.body;
  const data = await shoppingCartApi.changeCount({ count, _id });
  res.send(data);
});


router.post('/changeCount', async function (req, res, next) {
  const { count, _id } = req.body;
  const data = await shoppingCartApi.changeCount({ count, _id });
  res.send(data);
});
// 通过用户id查找对应购物车商品
router.get('/findCart', async function (req, res, next) {
  const { user_id } = req.query;
  const data = await shoppingCartApi.findCart(user_id);
  res.send(data);
});

// 通过商品id找到整条数据
router.get('/findComm', async function (req, res, next) {
  const { commodity_id } = req.query;
  const data = await shoppingCartApi.findComm(commodity_id);
  res.send(data);
});

// 暴露接口
module.exports = router;